package com.aitesting.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aitesting.mapper.UserMapper;
import com.aitesting.model.User;
import com.aitesting.service.UserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2022-02-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public SaTokenInfo login(String username, String password) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = this.getOne(queryWrapper);
        if (user == null) {
            return null;
        }
//        if (!SaSecureUtil.md5(password).equals(user.getPassword())) {
//            return null;
//        }
        if (!user.getPassword().equals(password)) {
            return null;
        }
        // 标记用户成功登录的状态
        StpUtil.login(user.getId());
        return StpUtil.getTokenInfo();
    }
}
